package com.SpringBoot.product.controller;


import com.SpringBoot.Util.Result;

import com.SpringBoot.product.pojo.Product;
import com.SpringBoot.product.service.ProductService;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;



/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author bxk
 * @since 2025-09-15
 */
@Controller
@RestController
@RequestMapping("/product")
public class ProductController {

    private final ProductService productService;

    public ProductController(ProductService productService) {
        this.productService = productService;
    }

    @PostMapping("/addProduct")
    public Result addProduct(@RequestBody Product product) {
        productService.addProduct(product);
        return Result.success(product);
    }

    @PostMapping("/updateProduct")
    public Result updateProduct(@RequestBody Product product) {
        productService.updateProduct(product);
        return Result.success(product);
    }

    @GetMapping("/getProduct")
    public Result getAllProduct(@RequestParam(value = "productId" , required = false) Integer productId,
                                @RequestParam(value = "productName" , required = false) String productName,
                                @RequestParam(value = "name" , required = false) String name) {
        return productService.getProduct(productId, productName, name);
    }

    @GetMapping("/selectProduct")
    public Result selectProduct(@RequestParam(value = "name" , required = false) String name ,
                                @RequestParam(value = "color" , required = false) String color){
        return productService.selectProduct(name, color);
    }

//    @GetMapping("/getProduct")
//    public Result getProduct(productDto productDto) {
//        try {
//            return productService.getProduct(productDto.getProductId());
//        } catch (Exception e) {
//            throw new RuntimeException(e);
//        }
//    }

    @PostMapping("/deletedProduct")
    public Result deletedProduct(@RequestParam("productId") Integer productId) {
        productService.deleteProduct(productId);
        return Result.success("删除成功");
    }

    @GetMapping("/allColor")
    public Result selecttColor(){
        return productService.selectColor();
    }

    @GetMapping("/allProductByPage")
    public Result allProductByPage(@RequestParam(required = false) String productName, @RequestParam(required = false) String name,
                                   @RequestParam Integer pageNum, @RequestParam Integer pageSize){
        return productService.allProductByPage(productName, name, pageNum, pageSize);
    }

    @GetMapping("/getCount")
    public Result getCount(){return productService.getCount();}
}
